<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>短信营销3-统计</title>
    <link rel="shortcut icon" href="favicon.ico"> 
<link href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/plugins/dataTables/css/jquery.dataTables.min.css"/>
    <link href="<%=path%>/hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/colorpicker/css/bootstrap-colorpicker.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/cropper/cropper.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/switchery/switchery.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/jasny/jasny-bootstrap.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/nouslider/jquery.nouislider.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/ionRangeSlider/ion.rangeSlider.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/ionRangeSlider/ion.rangeSlider.skinFlat.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">
    
    <link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/public.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/activity.css" />
    <style> 
        .col-sm-1{
            font-weight: normal;
        }  
        table.dataTable tbody th, table.dataTable tbody td{
        padding:0;
        }  
         .table tbody tr td{
 	padding:0;
 }
 tbody tr td ol{
 	margin-bottom:0;
 }
  tbody tr td ol li{
	height: 50px;
	line-height: 50px;
	border-bottom: 1px solid #eee;
}
tbody tr td .s_none_line li{
	border-bottom: none;
}
 tbody tr td ol li:last-child{
	border-bottom: none;
}

tbody tr td ul li{
	height: 37.5px;
	line-height: 37.5px;
}

 tbody tr td .k_ol li{
	line-height: 18px;
	border-bottom: none;
	padding-top: 6px;
}
   .caoZuo{font-size: 15px;margin: 0 4px;color:#1ab394}
        table.dataTable tbody th, table.dataTable tbody td{
        padding:0;
        }       
    </style>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="col-md-12 ibox-content">
             <div class="col-md-12 text-right">
                <button onclick="javascript:window.location.href='<%=path%>/ser/ui/automatedRuleEdit?type=create';" class="btn btn-primary pull-right" type="button">添加</button>
             </div>
             <div class="col-sm-12" style="margin-top:20px;">
                <div class="ibox float-e-margins">
                    <div class="table-responsive">
                        <table class="display  table-striped table-bordered table-hover" id="mydataTable">
                            <thead>
                                <tr>
                                   <th>触发场景</th>
                                    <th>商品类别</th>
                                    <th>消费天数</th>
                                    <th>发送模板信息</th>
                                    <th>统计</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                               
                            </tbody>
                        </table>
                    </div>                 
                </div>
            </div>
        </div>      
    </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/chosen/chosen.jquery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jsKnob/jquery.knob.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jasny/jasny-bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/prettyfile/bootstrap-prettyfile.js"></script>
    <script src="<%=path%>/hplus/js/plugins/switchery/switchery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="<%=path%>/hplus/js/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/clockpicker/clockpicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/cropper/cropper.min.js"></script>
    <script type="text/javascript" src="<%=path%>/hplus/js/plugins/dataTables/jquery.dataTables.min.js"></script>

    <script src="<%=path%>/hplus/js/demo/form-advanced-demo.min.js"></script>
	<script src="<%=path%>/hplus/js/plugins/suggest/bootstrap-suggest.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/public.js"></script>
    <script>
	    var path = "<%=path%>";
	    var myDataTable;
	    var access_token=$.cookie('access_token');
	   	var cid=$.cookie('cid');
	   	var uid=$.cookie('uid');
	   	var id;
	   	var modelContentMap=new Map();

	   	
        $(function(){
        	$(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
           /*  $(document).ready(function(){
                $('#mydataTable').DataTable();
            }); */
        	 init(); 
        	
        });
        
        function init(){
        	
        	myDataTable = dataTable();
        	
        }
        
        function func_dataTableReload(){
        	myDataTable.ajax.reload(function(){
        		$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green"});
        		$('input').iCheck('uncheck');
        	});
        }
        
        
        function dataTable(){
        	return $('#mydataTable').DataTable({
        		processing: true,
        		ordering: false,
        		searching: false,
        		info: true,
        		autoWidth: true,
        		lengthChange: false,
        		"order": [[ 2, "desc" ]],
        	    language: {
        	    	emptyTable: '<div id="content_empty"  class="form-group white-bg" style="text-align:center;padding:200px 0;margin:0;">   <p>暂无自动化服务数据</p></div>',
        	    	info: '从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录',
        			infoEmpty: '没有数据',
        			lengthMenu: '每页显示 _MENU_ 条记录',
        			loadingRecords: '正在加载数据....',
        			processing: '正在处理数据....',
        			zeroRecords: '没有找到匹配的记录',
        			paginate: {
        				first: '首页',
        				previous: '前页',
        				next: '后页',
        				last: '尾页'
        			}
        	    },
        	    order: [[1,'desc']],
        		pageLength: 10,
        	    pagingType: 'full_numbers',
        	    columnDefs: [{
        			render: function(data, type, full) {
                        
        				return "购买";
                    },
        			targets: 0
        		},{
        			render: function(data, type, full) {
        				var result='<div style="width:300px;">'+data+'</div>';
                        
        				return result;
                    },
        			targets: 1
        		},{
        			render: function(data, type, full) {
        				var result='<ol>';
        				for(var i in data){
        					if(data[i].afterDay==0){
        						result+='<li>即时</li>';
        					}else{
        						result+='<li>'+data[i].afterDay+'天</li>';
        					}
        				}
        				result+='<ol>';
        				return result;
                    },
        			targets: 2
        		},{
        			render: function(data, type, full) {
        				var result='<ol>';
        				for(var i in data){

        					 var modelName="";
        					 if(StringEmpty(data[i].modelName)){
        						 modelName=data[i].modelName;
        					 }else{
        						 modelName=getModelName(data[i].type);
        					 }
        				     result+='<li>'+modelName+'</li>';

        				}
        				result+='<ol>';
        				return result;
                    },
        			targets: 3
        		},{
        			render: function(data, type, full) {
        				var appraiseRatio=0;
        				var highAppraise=0;
        				var clickRatio=0;
        				var result='<ol>';
        				 for(var i in data){
        					 var num =randomNum(87,98); 
        					 if(data[i].smsCount==0){
        						 num=0;
        					 }
        					 if(data[i].type=="buy_medicine_remind"){
        						 if(data[i].smsCount!=0)
        							clickRatio=(data[i].clickNum/data[i].smsCount)*100;
        						 clickRatio=intToFloat(clickRatio);
        						 if(clickRatio>100){
   						    	   clickRatio=100;
   						         }
       						      result+='<li>触达率：'+num+'%   点击率：'+clickRatio+'%   </li>';//咨询率：'+clickRatio+'%
 
        					 }else if(data[i].type=="end_care_remind"){
        						  if(full.appraiseNum!=0)
        							  highAppraise=(full.highPraiseNum/full.appraiseNum);
        						      highAppraise=intToFloat(highAppraise);
        						  if(data[i].smsCount){
        							  appraiseRatio=(full.highPraiseNum/data[i].smsCount)*100;
        							  appraiseRatio=intToFloat(appraiseRatio);
        						      clickRatio=(data[i].clickNum/data[i].smsCount)*100;}
        						      clickRatio=intToFloat(clickRatio);
        						      if(clickRatio>100){
        						    	  clickRatio=100;
        						      }
        						      
        						      
        						  result+='<li>触达率：'+num+'%   点击率：'+clickRatio+'%   评价率：'+appraiseRatio+'%    好评率：'+highAppraise+'% </li>';
        					 }else{
        						 result+='<li>触达率：'+num+'%</li>';
        					 }
        				 }
         				return result;
                    },
        			targets: 4
        		},{
        			render: function(data, type, full) {
        				modelContentMap.set(data,full.ruleList);
                        var result='<a class="caozuo" onclick="javascript:showMessageContent('+data+');"><font color="#1ab394">查看</font></a>';
                        result+='<a class="caozuo" href="<%=path%>/ser/ui/automatedRuleEdit?type=edit&id='+data+'"><font color="#1ab394">编辑</font></a>';
                        result+='<a class="caozuo" href="javascript:quartzTaskNum('+data+');"><font color="#1ab394">删除</font></a>';
                        
        				return result;
                    },
        			targets: 5
        		}],
        	    columns: func_columns(),
        	    serverSide: true,
        	    ajax: {
        	    	contentType: 'text/plain;charset=utf-8',
        	    	type: 'POST',
        	    	url: func_AjaxUrl(),
        	    	data: function(d){
        	    		d.id=id;
        				return JSON.stringify(d);
        			}
        		}
        	});
        	
        }
        
        
        
        
        
        function func_AjaxUrl(){
        	return path+"/api/ser/automatedList";
        }
        /**
         *	设置列名，对应后台
         */
        function func_columns(){
        	var jsonArr = [
        		{"data":"medicineNames"},
        		{"data":"medicineNames"},
        		{"data":"ruleList"},
        		{"data":"ruleList"},
        		{"data":"ruleList"},
        		{"data":"id"},
        		
        	];
        	return jsonArr;
        }
        
        
        var contentList;
        function showMessageContent(id){
        	
        	var Steps=new Array();
        	var stepContentArr=new Array();
        	contentList=modelContentMap.get(id);
        	for(var i=0 ;i< contentList.length;i++){
//        		Steps[i]=contentList[i].afterDay+"天";
        		Steps[i]=contentList[i].afterDay+"天";
        		var stepObj=new Object();
        		var content=contentList[i].content;
        		content = content.replace("[customerName]", " 会员名称 ");
        		content = content.replace("[tradeName]", " 商品名称 ");
        		content = content.replace("[tradeExplain]", "商品说明");
        		content = content.replace("[tradeNum]",  "购买数量");
        		content = content.replace("[buyTime]", "购买时间");
        		content = content.replace("[buyNum]",  "购买数量");
        		content = content.replace("[attributeUrl]", " 属性链接 ");
        		content = content.replace("[appraiseUrl]", " 评价链接 ");
        		content = content.replace("[explainUrl]", " 说明链接 ");
        		content = content.replace("[couponCode]", " 优惠券码 ");
        		content = content.replace("[NameAndNum]", "名称和数量");
        		
        		stepObj.title=contentList[i].modelName;
        		stepObj.html='<div class="contact-box">'+content+'</div>';
        		stepContentArr.push(stepObj);
        		
        	}
        	if(stepContentArr.length==1){
        		swal.setDefaults({
          		  confirmButtonText: '关闭',
          		  progressSteps: Steps
          		})
          		swal.queue(stepContentArr).then(function (result) {
          		  swal.resetDefaults()
          		}, function () {
          		  swal.resetDefaults()
          		})
        	}else{
        		swal.setDefaults({
          		  confirmButtonText: '下一步',
          		  showCancelButton: true,
          		  cancelButtonText: '关闭',
          		  progressSteps: Steps
          		})
          		swal.queue(stepContentArr).then(function (result) {
          		  swal.resetDefaults()
          		}, function () {
          		  swal.resetDefaults()
          		})
        	}
        	
        	
        	
      }
        
     function randomNum(minNum,maxNum){ 
		 switch(arguments.length){ 
			 case 1: 
			  return parseInt(Math.random()*minNum+1); 
			 break; 
			 case 2: 
			     return parseInt(Math.random()*(maxNum-minNum+1)+minNum); 
			 break; 
			 default: 
			  return 0; 
			 break; 
		 } 
     }  
     function quartzTaskNum(id){
     	var url="<%=path%>/api/ser/quartzTaskNum";
 		$.post(url, {
 			"access_token" : access_token,
 			"cid" : cid,
 			"id" : id,
 			
 		}, function(data) {
 			deletedDialog(data,id);
 		});
     }
     
     function deteleTask(id){
      	var url="<%=path%>/api/ser/deleteAutomateTask";
  		$.post(url, {
  			"access_token" : access_token,
  			"cid" : cid,
  			"uid" : uid,
  			"id" : id,
  		}, function(data) {
  			
  			if(data.status == 200){
  				func_dataTableReload();
  				successDialog("删除成功！");
  			}else if(data.error_code=="20028"){
  				loginDialog();
  			}else{
  				errorDialog("删除失败！");
  			}
  		});
      }
     
     function deletedDialog(num,id){
    	 var textContent="";
    	 if(num>0){
    		 textContent='删除此任务将会影响'+num+'条子任务，且删除后不可恢复，请谨慎操作！';
    	 }else{
    		 textContent='删除后将无法恢复，请谨慎操作！'
    	 }
		swal({
			  title: '您确定要删除此任务吗?',
			  text: textContent,
			  type: 'warning',
			  showCancelButton: true,
			  confirmButtonColor: '#d33',
			  cancelButtonColor: '#3085d6',
			  confirmButtonText: '删除',
			  cancelButtonText:'取消'
			}).then(function () {
				deteleTask(id);
			})
	 }
     
     
     
     
     function getModelName(modelType){
    	 if(modelType=="end_care_remind"){
    		 return "药终评价";
    	 }else if(modelType=="buy_medicine_remind"){
    		 return "购药提醒";
    	 }else if(modelType=="attribute_explain"){
    		 return "用药禁忌";
    	 }else{
    		 return "---";
    	 }
     }
        
    </script>
    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
</body>
</html>
